home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / USAGEMAN.C < prev    next >
Text File  |  1992-12-02  |  11KB  |  471 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK    012490    :    Added no man avail. message.
  54.  *    SPK 012290    :    Initial
  55.  */
  56.  
  57. #include    <ResourceMgr.h>
  58.  
  59. #include    "SystemPub.h"
  60. #include    "Proc.h"
  61. #include    "ShellPub.h"
  62. #include    "Path.h"
  63.  
  64. extern    int16    appResFile;
  65.  
  66. /*******************************************************************/
  67.  
  68. #define        MUechomode        (**MyShell).Proc[ProcID].bflags.f0
  69.  
  70. DumpResText(  WHandle ShellWh, int16 ProcID, char *name, int32 type )
  71. {
  72. char    buf[ 256 ], *cp, **ch;
  73. int16    i;
  74. int32    length;
  75.  
  76.     strcpy( buf, name );
  77.     CtoPstr( buf );
  78.     UseResFile( appResFile );
  79.     
  80.     ch = (char **) GetNamedResource( type, buf );
  81.     LoadResource( ch );
  82.     
  83.     if( ch )
  84.         {
  85. DoResTextDump:        /* a code saving goto */
  86.  
  87.         HLock( ch );
  88.         
  89.         cp = *ch;
  90.         length = GetHandleSize( ch );
  91.         
  92.         while( length )
  93.             {
  94.             i = 0;
  95.             while( i < 255 )
  96.                 {
  97.                 buf[ i ] = *cp;
  98.                 
  99.                 if( (*cp == '\n') || (*cp == '\r') )
  100.                     {
  101.                     buf[ ++i ] = '\0';
  102.                     cp++;
  103.                     length--;
  104.                     StdOut( ShellWh, ProcID, buf );
  105.                     break;
  106.                     }
  107.                 i++;
  108.                 cp++;
  109.                 length--;
  110.                 }
  111.                 
  112.             if( i == 255 )    /* filled the buffer */
  113.                 {
  114.                 buf[ ++i ] = '\0';
  115.                 StdOut( ShellWh, ProcID, buf );
  116.                 }
  117.             }
  118.         
  119.         HUnlock( ch );
  120.         }
  121.     else    /* was it an alias ? */
  122.         {
  123.         ch = GetShellString( name );
  124.         
  125.         if( ch )
  126.             {
  127.             CopyStr( *ch, buf );
  128.             
  129.             buf[0]--;
  130.             ch = (char **) GetNamedResource( type, buf );
  131.             
  132.             if( ch )
  133.                 {
  134.                 StdOut( ShellWh, ProcID, "ALIAS COMMAND\n" );
  135.                 goto DoResTextDump;
  136.                 }
  137.             else
  138.                 goto NoResText;    /* save a little code */
  139.             }
  140.         else
  141.             {
  142. NoResText:
  143.             procPrintf( ShellWh, ProcID, "man : no manual for %s\n",
  144.                     name );
  145.             }
  146.         }
  147. }
  148.  
  149. /*******************************************************************/
  150.  
  151. Boolean            DoUSAGE( int16 ProcToken, WHandle ShellWh, int16 ProcID,
  152.                     char *string )
  153. {
  154. int16                i, argc;
  155. char            *cp, argument[ 256 ];
  156. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  157.  
  158.     switch( ProcToken )
  159.         {
  160.         case    PROC_INIT    :
  161.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  162.             break;
  163.             
  164.         case    PROC_TERM    :
  165.         case    PROC_BREAK    :
  166.             /* Tell the shell that we're done */
  167.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  168.             /* Turn ourself off */
  169.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  170.             break;
  171.             
  172.         case    PROC_STDIN    :
  173.             if( (**MyShell).Proc[ ProcID ].flags )
  174.                 {
  175.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  176.  
  177.                 if( argc == 1 )
  178.                     DumpResText( ShellWh, ProcID, "usage", MANRES );
  179.                 else
  180.                     for( i = 1; i < (**MyShell).Proc[ ProcID ].argc; i++ )
  181.                         {
  182.                         GetArgv( ShellWh, ProcID, i, argument );
  183.                         DumpResText( ShellWh, ProcID, argument, USERES );
  184.                         }
  185.  
  186.                 /* Tell the shell that we're done */
  187.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  188.                 
  189.                 /* Turn ourself off */
  190.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  191.                 return( FALSE );
  192.                 }
  193.         }
  194. }
  195.  
  196. /*******************************************************************/
  197.  
  198. WipeMANS()
  199. {
  200. int16    i;
  201. Handle    h;
  202.  
  203.     UseResFile( appResFile );
  204.     i = Count1Resources( MANRES );
  205.     
  206.     while( i )
  207.         {
  208.         h = GetIndResource( MANRES, i );
  209.         RmveResource( h );
  210.         DisposHandle( h );
  211.         i--;
  212.         }
  213.         
  214.     i = Count1Resources( USERES );
  215.     
  216.     while( i )
  217.         {
  218.         h = GetIndResource( USERES, i );
  219.         RmveResource( h );
  220.         DisposHandle( h );
  221.         i--;
  222.         }
  223. }
  224.  
  225. /*******************************************************************/
  226.  
  227. #define        BUFSIZE    32
  228.  
  229. void        MANCCallBack( WHandle ShellWh, int16 ProcID, char *path,
  230.                 char *last, pathType what, int16 vRefNum, int32 dirID )
  231. {
  232. char        *cp, str[ BUFSIZE ], name[ 32 ];
  233. int16        fRefNum, sErr = noErr, id;
  234. FInfo        finder;
  235. int32        count = 1L, resSize, start, end, type = 0L;
  236. Handle        h;
  237. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  238.  
  239.     if( what == pathIsFile )
  240.         {
  241.         strcpy( str, last );
  242.         CtoPstr( str );
  243.     
  244.         sErr = HGetFInfo( vRefNum, dirID, str, &finder );
  245.         
  246.         if( !sErr && (finder.fdType == 'TEXT' ))
  247.             sErr = HOpen( vRefNum, dirID, str, fsRdPerm, &fRefNum );
  248.  
  249.         
  250.         while( !sErr && !UserAbort())
  251.             {
  252.             count = 1L;
  253.             cp = str;
  254.             
  255.             sErr = FSRead( fRefNum, &count, cp );
  256.             CursorWait();
  257.         
  258.             if( !sErr )
  259.                 {
  260.                 if( *cp == '#' )    /* comment, read until next line */
  261.                     {
  262.                     while( !sErr )
  263.                         {
  264.                         sErr = FSRead( fRefNum, &count, cp );
  265.                         if( sErr || (*cp == '\n') || (*cp == '\r') )
  266.                             break;
  267.                         }
  268.                     }
  269.                 else if( *cp == '<' )
  270.                     {
  271.                     sErr = FSRead( fRefNum, &count, cp );
  272.                     if( !sErr )
  273.                         {
  274.                         if( *cp == 'U' )        /* usage specification */
  275.                             type = USERES;
  276.                         else if( *cp == 'M' )    /* man specification */
  277.                             type = MANRES;
  278.                         
  279.                         if( type )
  280.                             {    
  281.                             while( !sErr )        /* read past '> ' to name */
  282.                                 {
  283.                                 sErr = FSRead( fRefNum, &count, cp );
  284.                                 if( sErr || ((*cp != ' ') && (*cp != '>')) )
  285.                                     break;
  286.                                 }
  287.                             *name = *cp;
  288.                             cp = name;
  289.                             cp++;
  290.                             
  291.                             while( !sErr )        /* read name */
  292.                                 {
  293.                                 sErr = FSRead( fRefNum, &count, cp );
  294.                                 if( sErr || (*cp == '\n') || (*cp == '\r') )
  295.                                     break;
  296.                                     
  297.                                 cp++;
  298.                                 }
  299.                                 
  300.                             *cp = '\0';
  301.                             CtoPstr( name );    /* use name */
  302.                             cp = str;
  303.                             
  304.                             sErr = GetFPos( fRefNum, &start );
  305.                             
  306.                             sErr = FSRead( fRefNum, &count, cp );
  307.                             while( !sErr )        /* go till next command */
  308.                                 {
  309.                                 if( !sErr && ((*cp == '\n') || (*cp == '\r')) )
  310.                                     {
  311.                                     sErr = FSRead( fRefNum, &count, cp );
  312.                                     if( sErr || (*cp == '#') || (*cp == '<') )
  313.                                         break;
  314.                                     }
  315.                                 else
  316.                                     sErr = FSRead( fRefNum, &count, cp );
  317.                                 }
  318.                             if( !sErr )
  319.                                 {
  320.                                 GetFPos( fRefNum, &end );
  321.                                 resSize = end - start - 1;
  322.                                 sErr = SetFPos( fRefNum, fsFromStart, start );
  323.                                 
  324.                                 if( !sErr )
  325.                                     {
  326.                                     h = NewHandle( resSize );
  327.                                     
  328.                                     if( h )
  329.                                         {
  330.                                         HLock( h );
  331.                                         cp = *h;
  332.                                         sErr = FSRead( fRefNum, &resSize, cp );
  333.                                         HUnlock( h );
  334.                                         
  335.                                         if( !sErr )
  336.                                             {
  337.                                             UseResFile( appResFile );
  338.                                             id = Unique1ID( type );
  339.                                             
  340.                                             SetResAttrs( h, resPurgeable );
  341.                                             AddResource( h, type, id, name );
  342.                                             }
  343.                                         else
  344.                                             DisposHandle( h );
  345.                                         
  346.                                         if( MUechomode )
  347.                                             procPrintf( ShellWh, ProcID,
  348.                                                 "%6s %16ps ID %5d\n",
  349.                                                 type == USERES ? "Usage" : "Man",
  350.                                                 name, id );
  351.                                         }
  352.                                     }
  353.                                 }
  354.                             }/* eo if type */
  355.                         }
  356.                     } /* eo if '<' */
  357.                 }
  358.             }
  359.                 
  360.                 
  361.         if( fRefNum )    /* if a file open, close it */
  362.             FSClose( fRefNum );
  363.         }
  364. }
  365.  
  366.  
  367. /*******************************************************************/
  368.  
  369. CompileMANS( WHandle ShellWh, int16 ProcID, char *argument )
  370. {
  371. ShellWindRec    **MyShell;
  372.  
  373.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  374.  
  375.     ExpandPath( ShellWh, ProcID, argument, (ProcPtr) MANCCallBack,
  376.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  377.  
  378.     ResetShellPWD( ShellWh );
  379. }
  380.  
  381. /*******************************************************************/
  382.  
  383. Boolean            DoMAN( int16 ProcToken, WHandle ShellWh, int16 ProcID,
  384.                     char *string )
  385. {
  386. int16            i, argc, wmode = FALSE, cmode = FALSE;
  387. char            *cp, argument[ 256 ];
  388. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  389.  
  390.     switch( ProcToken )
  391.         {
  392.         case    PROC_INIT    :
  393.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  394.             break;
  395.             
  396.         case    PROC_TERM    :
  397.         case    PROC_BREAK    :
  398.             /* Tell the shell that we're done */
  399.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  400.             /* Turn ourself off */
  401.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  402.             break;
  403.             
  404.         case    PROC_STDIN    :
  405.             if( (**MyShell).Proc[ ProcID ].flags )
  406.                 {
  407.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  408.  
  409.                 argc = (**MyShell).Proc[ ProcID ].argc;
  410.                 MUechomode = FALSE;
  411.                 
  412.                 if( argc == 1 )
  413.                     DumpResText( ShellWh, ProcID, "man", MANRES );
  414.                 else
  415.                     {
  416.                     for( i = 1; i < argc; i++ )
  417.                         {
  418.                         GetArgv( ShellWh, ProcID, i, argument );
  419.                         cp = argument;
  420.             
  421.                         if( *cp++ == '-' )
  422.                             {
  423.                             if( *cp == '\0' )    /* read from stdin */
  424.                                 (**MyShell).Proc[ ProcID ].options = 1L;
  425.                             else
  426.                                 while( *cp )
  427.                                     switch( *cp++ )
  428.                                         {
  429.                                         case    'c'    :    /* compile file */
  430.                                             cmode = TRUE;
  431.                                             break;
  432.                                         case    'w'    :    /* wipe existin mans/usages */
  433.                                             wmode = TRUE;
  434.                                             break;
  435.                                         case    'e'    :    /* echo lines */
  436.                                             MUechomode = TRUE;
  437.                                             break;
  438.                                         }
  439.                             }
  440.                         }
  441.                         
  442.                     if( wmode || cmode )
  443.                         {
  444.                         if( wmode )
  445.                             WipeMANS();
  446.                         if( cmode )
  447.                             for( i = 1; i < argc; i++ )
  448.                                 {
  449.                                 GetArgv( ShellWh, ProcID, i, argument );
  450.                                 if( *argument != '-' )
  451.                                     CompileMANS( ShellWh, ProcID, argument );
  452.                                 }
  453.                         }
  454.                     else
  455.                         for( i = 1; i < argc; i++ )
  456.                             {
  457.                             GetArgv( ShellWh, ProcID, i, argument );
  458.                             DumpResText( ShellWh, ProcID, argument, MANRES );
  459.                             }
  460.                     }
  461.  
  462.                 /* Tell the shell that we're done */
  463.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  464.                 
  465.                 /* Turn ourself off */
  466.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  467.                 return( FALSE );
  468.                 }
  469.         }
  470. }
  471.